package com.health.controller.manage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.common.PageBean;
import com.health.common.QueryParam;
import com.health.common.R;
import com.health.pojo.CheckGroup;
import com.health.pojo.CheckItem;
import com.health.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // 将类交给SpringBoot管理
@RequestMapping("/api/manage/checkGroup")  //该模块通用路径前缀
@Api(tags = "检查组管理")
@RequiredArgsConstructor //构造器注入，替换@Autowird
@Slf4j  //日志输出类
public class CheckGroupController {

    private final CheckGroupService checkGroupService;

    @ApiOperation(value = "分页条件查询")
    @GetMapping("/pageQuery")
    public R pageQuery(QueryParam param) {
        log.info("分页条件查询 {}", param);
        Page<CheckGroup> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        checkGroupService.query().like(param.getQueryString() != null, "name", param.getQueryString()).or().like(param.getQueryString() != null, "code", param.getQueryString()).or().like(param.getQueryString() != null, "help_code", param.getQueryString()).page(page);
        PageBean pageBean = new PageBean(page.getRecords(), page.getTotal());
        return R.success(pageBean);
    }

    @PostMapping
    @ApiOperation(value = "添加检查组")
    public R addCheckGroup(@RequestBody CheckGroup checkGroup,
                           @RequestParam(value = "ids") Integer[] checkItemIds) {
        log.info("添加检查组 {} {}", checkGroup, checkItemIds);
        checkGroupService.addCheckGroup(checkGroup, checkItemIds);
        return R.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public R getById(@PathVariable Integer id) {
        log.info("根据id查询 {}", id);
        CheckGroup checkGroup = checkGroupService.getById(id);
        return R.success(checkGroup);
    }


    @GetMapping("/getCheckItemsById/{id}")
    @ApiOperation(value = "查询检查组对应检查项列表")
    public R getCheckItemsById(@PathVariable Integer id) {
        log.info("查询检查组对应检查项列表 {}", id);
        //List<CheckItem>
        List<CheckItem> list = checkGroupService.getCheckItemsById(id);
        return R.success(list);
    }

    @PutMapping
    @ApiOperation(value = "修改检查组")
    public R updateCheckGroup(@RequestBody CheckGroup checkGroup,
                              @RequestParam("ids") Integer[] checkItemIds) {
        log.info("修改检查组 {} {}", checkGroup, checkItemIds);
        checkGroupService.updateCheckGroup(checkGroup, checkItemIds);
        return R.success();
    }

//    @ApiOperation(value = "删除检查组")
    @DeleteMapping("/{id}")
    public R deleteCheckGroup(@PathVariable Integer id) {
        log.info("删除检查组 {}", id);
        checkGroupService.deleteCheckGroup(id);
        return R.success();
    }

    @ApiOperation(value = "查询所有检查组")
    @GetMapping("/list")
    public R getCheckGroupList() {
        log.info("查询所有检查组");
        List<CheckGroup> list = checkGroupService.query().list();
        return R.success(list);
    }
}
